import { postAction, getAction } from '@/utils/httpUtil'
import { dateFormat, filterObj } from '@/utils/public'
import { debounce } from '@/utils/public'
export default {
	data() {
		return {
			queryFormData: {},
			defaultQueryParams: {},
			loading: false,
			dataSource: [],
			tableHeight: 100,
			isPage: true,
			pageParams: {
				currentPage: 1,
				total: 0,
				pageSize: 20,
				pageSizes: [20, 50, 100, 200],
			},
			apiUrl: {},
			selectRow: {}, // 单选
			multipleSelection: [], // 多选
			radioSelect: '',
			formBtnText: '展开',
			autoLoadData: true,
			showQueryItem: false,
		}
	},
	mounted() {
		if (this.autoLoadData) {
			this.loadData()
		}
		window.addEventListener('resize', debounce(this.getTableHeight, 100))
	},
	destroyed() {
		window.removeEventListener('resize', this.getTableHeight)
	},
	methods: {
		dateFormat,
		resetQueryForm() {
			this.$refs.queryForm.resetFields()
			this.loadData(1)
		},
		/**
		 * 展示更多
		 */
		showMore() {
			this.showQueryItem = !this.showQueryItem
			if (this.showQueryItem) {
				this.formBtnText = '收起'
			} else {
				this.formBtnText = '展开'
			}
			this.$nextTick(() => {
				this.getTableHeight()
			})
		},
		/**
		 * 加载数据
		 */
		loadData(page, callback) {
			if (page === 1) {
				this.pageParams.currentPage = 1
			}
			this.loading = true
			getAction(this.apiUrl.list, this.getQueryParams())
				.then(res => {
					this.dataSource = []
					if (res.success) {
						if (this.isPage) {
							if (res.info) {
								this.dataSource = res.info
							}
							this.pageParams.total = res.totalCount
						} else {
							if (res.info) {
								this.dataSource = res.info
							}
						}
						if (callback) {
							callback()
						}
					}
					this.loading = false
				})
				.catch(() => {
					this.loading = false
				})
		},
		getQueryParams() {
			let param = Object.assign({}, this.queryFormData, this.defaultQueryParams, this.queryParam)
			param.start = this.pageParams.currentPage - 1
			param.limit = this.pageParams.pageSize
			return filterObj(param)
		},
		/**
		 * 分页总条数变化监听
		 */
		handleSizeChange(val) {
			this.pageParams.pageSize = val
			this.loadData(1)
		},
		/**
		 * 点击当前页
		 */
		handleCurrentChange(val) {
			this.pageParams.currentPage = val
			this.loadData()
		},
		/**
		 * 多选。选择变化
		 * @param {*} val
		 */
		handleSelectionChange(val) {
			this.multipleSelection = val
		},
		/**
		 * 计算表格高度
		 */
		// getTableHeight() {},
		rowRadioClick(row, colum, event) {
			if (this.radioSelect === '' || this.radioSelect !== row.id) {
				this.radioSelect = row.id
			}
			this.selectRow = row
		},
		handleRadioRowChange(row) {
			if (this.radioSelect === '' || this.radioSelect !== row.id) {
				// 解决事件冲突
				this.radioSelect = row.id
				// console.log('radioSelect=' + this.radioSelect);
			}
			this.selectRow = row
		},
		rowBtnHasPerm(permList, other = true) {
			let hasPermi = false
			let permissions = this.$route.meta.permi

			if (permissions && permissions.length > 0) {
				let permi = permissions.some(per => {
					return permList.includes(per)
				})
				if (permi && other) {
					hasPermi = true
				}
			}
			// console.log('==是否有权限==' + hasPermi, permList)
			return hasPermi
		},
	},
}
